package pswvclass.pswvandroid2.com.risk;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import pswvclass.pswvandroid2.R;
import pswvclass.pswvandroid2.com.kwords.KwordList;
import pswvclass.pswvandroid2.com.util.HttpUtils;

/**
 * Created by Administrator on 2018/11/2.
 */
public class RiskEdit extends Fragment implements View.OnClickListener {

    protected static final int EDIT_SUCCESS = 0;
    protected static final int NO_EDIT = 1;
    private String tag = "RiskEdit";
    private TextView kWords;
    private TextView content;
    private Button edit_sub;
    private Button edit_home;
    private int editid;
    private String kWords1;
    private String content1;

    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case EDIT_SUCCESS:
                    Toast.makeText(getContext(), "修改成功", Toast.LENGTH_LONG).show();
                    break;
                case NO_EDIT:
                    Toast.makeText(getContext(), "修改失败", Toast.LENGTH_LONG).show();
                    break;
                default:
                    break;
            }

        }

        ;

    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        return inflater.inflate(R.layout.pswv_risk_edit, null);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            editid = bundle.getInt("editid");
            String kWords1 = bundle.getString("kWord");
            String content1 = bundle.getString("content");
            kWords = (EditText) getView().findViewById(R.id.edit_kwords);
            content = (EditText) getView().findViewById(R.id.edit_content);
            kWords.setText(kWords1);
            content.setText(content1);
        }
        edit_sub = (Button) getView().findViewById(R.id.edit_sub);
        edit_sub.setOnClickListener(this);
    }


    @Override
    public void onClick(View view) {
        final int id = view.getId();
        switch (id) {
            case R.id.edit_sub:
                new Thread() {
                    @Override
                    public void run() {
                        try {
                            HttpUtils hu = new HttpUtils();
                            String data2;
                            final String src = getResources().getString(R.string.src_mysql) + "pswv/api/riskAction/edit";
                            final Map<String, String> map1 = new HashMap<String, String>();
                            map1.put("id", editid + "");
                            map1.put("kWord", kWords.getText().toString().trim());
                            map1.put("content", content.getText().toString().trim());

                            data2 = hu.submitPostData(src, map1, "utf-8");
                            JSONObject jsonObj = new JSONObject(data2);
                            int result = Integer.parseInt(jsonObj.getString("code"));
                            if (result == 0) {
                                Message msg = Message.obtain();
                                msg.what = EDIT_SUCCESS;
                                mHandler.sendMessage(msg);
                            } else {
                                Message msg = Message.obtain();
                                msg.what = NO_EDIT;
                                mHandler.sendMessage(msg);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            Log.v(tag, e + "");
                        }
                    }
                }.start();
                break;
            default:
                break;
        }
    }
}
